package com.kkang.netpan.service;

import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

@Service
public class NetpanService {
    /*
     *
     * 查看指定目录下的所有文件和目录操作
     *
     * */
    public File[] scanFileOrDir(String filepat) {
            return new File(filepat).listFiles();
    }

    /*
     * 输出二进制文件流
     * */
    public  InputStream printcontent(File file) throws IOException {
        return  new FileInputStream(file);
    }


    /*
    * shell 切换盘符操作
    * */
    public void shellCommand(String cmd , String Catalogue) throws IOException {
        switch (cmd) {
            case "..":
                Catalogue = new File(Catalogue).getParent();
                break;
            case "/":
                Catalogue = "";
                break;
            case "cat":
                printcontent(new File(Catalogue));
                break;
            default:
                if (Catalogue.isEmpty()) {
                    Catalogue += cmd;
                } else {
                    List<String> filelist = Arrays.asList(new File(Catalogue).list());
                    if (filelist.indexOf(cmd) != -1) {
                        Catalogue += cmd;
                    } else {
                        System.out.println("指令错误");
                    }

                }

                break;
        }
    }

}

